import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-14  20:56
 */
public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {
        DataSource datascource = new MysqlDataSource();
        ((MysqlDataSource) datascource).setUrl("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&&useSLL=false");
        ((MysqlDataSource) datascource).setUser("root");
        ((MysqlDataSource) datascource).setPassword("mysql密码");

        Connection connection = datascource.getConnection();

        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + " name: " + name);
        }

        resultSet.close();
        statement.close();
        connection.close();
    }
}
